package me.zk.practice1.s;

public class S_14_LongestCommonPrefix {
    public static String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        if (strs.length == 1) {
            return strs[0];
        }
        String s = strs[0];
        for (int i = 1; i < strs.length ; i++) {
            s = compare(s,strs[i]);
        }
        return s;
    }
    public static String compare(String s1, String s2) {
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (s1.length() > i && s2.length()>i ) {
            if (s1.charAt(i) == s2.charAt(i)) {
                sb.append(s1.charAt(i));
            } else {
                break;
            }
            i++;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String[] strs = new String[]{"flower","flow","flight"};
        System.out.println(longestCommonPrefix(strs));
    }

}
